package com.example.partner.fragment.find;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import com.example.partner.R;
import com.example.partner.activity.find.PublishPostActivity;
import com.example.partner.adapter.find.FindPagerAdapter;
import com.example.partner.databinding.FragmentFindBinding;
import com.example.partner.fragment.CommonFragment;
import com.example.partner.fragment.MatchFragment;
import com.example.partner.fragment.NearFragment;
import com.example.partner.fragment.find.FellowFragment;
import com.example.partner.fragment.find.MyPublishFragment;
import com.example.partner.fragment.find.RecommendFragment;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class FindFragment extends Fragment {

    private FragmentFindBinding binding;
    private TabLayout tabLayout;
    private ViewPager2 viewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentFindBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        tabLayout = view.findViewById(R.id.findTab);
        viewPager = view.findViewById(R.id.FindViewPager);
        // 设置ViewPager2的适配器
        viewPager.setAdapter(new FindPagerAdapter(this));
        // 将TabLayout与ViewPager2关联
        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> {
                    // 设置Tab的标题
                    switch (position) {
                        case 0:
                            tab.setText("推荐");
                            break;
                        case 1:
                            tab.setText("我关注的");
                            break;
                        case 2:
                            tab.setText("我发布的");
                            break;
                    }
                }).attach();
        // 添加 TabLayout 的 Tab 选择监听器
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Fragment selectedFragment = null;
                switch (tab.getPosition()) {
                    case 0:
                        selectedFragment = new RecommendFragment();
                        break;
                    case 1:
                        selectedFragment = new FellowFragment();
                        break;
                    case 2:
                        selectedFragment = new MyPublishFragment();

                        break;
                }
                if (selectedFragment != null) {
                    // 使用 FragmentManager 来切换 Fragment
                    //getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        FloatingActionButton fabCreatePost = view.findViewById(R.id.fabCreatePost);
        fabCreatePost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 在这里处理点击事件，启动创建帖子页面的 Intent
                Intent intent = new Intent(requireContext(), PublishPostActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        binding = null;
    }
}